package com.milhouz.pokertimer.constant;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

public class PriceRateConstant {

	/**
	 * TODO Fill PRICE_RATE from xml file.
	 * Integer = nb winners
	 * List    = list of rates by places
	 */
	public static Map<Integer,LinkedList<Map<Integer, Double>>> PRICE_RATE;
	
	static {
		PRICE_RATE = new HashMap<Integer, LinkedList<Map<Integer, Double>>>();
		
		// 1 winner
		LinkedList<Map<Integer, Double>> list = new LinkedList<Map<Integer, Double>>();
		Map<Integer, Double> rates = new HashMap<Integer, Double>();
		rates.put(1, 100d);
		list.add(rates);
		PRICE_RATE.put(1, list);
		
		// 2 winners
		list = new LinkedList<Map<Integer, Double>>();
		rates = new HashMap<Integer, Double>();
		rates.put(1, 90d);
		rates.put(2, 10d);
		list.add(rates);
		rates = new HashMap<Integer, Double>();
		rates.put(1, 80d);
		rates.put(2, 20d);
		list.add(rates);
		rates = new HashMap<Integer, Double>();
		rates.put(1, 70d);
		rates.put(2, 30d);
		list.add(rates);
		rates = new HashMap<Integer, Double>();
		rates.put(1, 60d);
		rates.put(2, 40d);
		list.add(rates);
		PRICE_RATE.put(2, list);
		
		// 3 winners
		list = new LinkedList<Map<Integer, Double>>();
		rates = new HashMap<Integer, Double>();
		rates.put(1, 80d);
		rates.put(2, 10d);
		rates.put(3, 10d);
		list.add(rates);
		rates = new HashMap<Integer, Double>();
		rates.put(1, 70d);
		rates.put(2, 20d);
		rates.put(3, 10d);
		list.add(rates);
		rates = new HashMap<Integer, Double>();
		rates.put(1, 60d);
		rates.put(2, 20d);
		rates.put(3, 20d);
		list.add(rates);
		rates = new HashMap<Integer, Double>();
		rates.put(1, 60d);
		rates.put(2, 30d);
		rates.put(3, 10d);
		list.add(rates);
		PRICE_RATE.put(3, list);
		
		// 4 winners
		list = new LinkedList<Map<Integer, Double>>();
		rates = new HashMap<Integer, Double>();
		rates.put(1, 70d);
		rates.put(2, 10d);
		rates.put(3, 10d);
		rates.put(4, 10d);
		list.add(rates);
		rates = new HashMap<Integer, Double>();
		rates.put(1, 60d);
		rates.put(2, 20d);
		rates.put(3, 10d);
		rates.put(4, 10d);
		list.add(rates);
		rates = new HashMap<Integer, Double>();
		rates.put(1, 50d);
		rates.put(2, 20d);
		rates.put(3, 20d);
		rates.put(4, 10d);
		list.add(rates);
		rates = new HashMap<Integer, Double>();
		rates.put(1, 50d);
		rates.put(2, 25d);
		rates.put(3, 15d);
		rates.put(4, 10d);
		list.add(rates);
		PRICE_RATE.put(4, list);
	}
	
	public static CharSequence[] getPatternsByNbWinners(int nbWinners) {
		CharSequence[] patternLabels;
		if (nbWinners > 0 && nbWinners <= 4) {
			LinkedList<Map<Integer, Double>> patterns = PriceRateConstant.PRICE_RATE.get(new Integer(nbWinners));
			patternLabels = new String[patterns.size()];
			int i = 0;
			for (Map<Integer, Double> pattern : patterns) {
				for (Integer place : pattern.keySet()) {
					if (patternLabels[i] != null)
						patternLabels[i] = patternLabels[i].toString() + " - " + String.format("%.0f", pattern.get(place)) + "%";
					else
						patternLabels[i] = String.format("%.0f", pattern.get(place)) + "%";
				}
				i++;
			}
		} else {
			patternLabels = new String[1];
			patternLabels[0] = String.format("%.0f", (float) 100 / nbWinners);
		}
		return patternLabels;
	}
	
	public static CharSequence[] getPatternsIndexByNbWinners(int nbWinners) {
		CharSequence[] patternIndex;
		if (nbWinners > 0 && nbWinners <= 4) {
			LinkedList<Map<Integer, Double>> patterns = PriceRateConstant.PRICE_RATE.get(new Integer(nbWinners));
			patternIndex = new String[patterns.size()];
			for (int i = 0; i < patterns.size(); i++) {
				patternIndex[i] = String.valueOf(i);
			}
		} else {
			patternIndex = new String[1];
			patternIndex[0] = String.format("%.0f",(float) 100 / nbWinners);
		}
		return patternIndex;
	}
	
	public static CharSequence getPercentsByNbWinnerAndSelectedPattern(int nbWinners, int selectedPattern) {
		Map<Integer,Double> patterns;
		CharSequence pattern = "";
		if (nbWinners > 0 && nbWinners <= 4) {
			if (selectedPattern < PRICE_RATE.get(nbWinners).size()) {
				patterns = PRICE_RATE.get(nbWinners).get(selectedPattern);
				for (Integer key : patterns.keySet()) {
					pattern = pattern + key.toString() + ". " + patterns.get(key) + "% ";
				}
			}
		}
		return pattern;
	}
}
